// Package layer_range_tree 6-515
// @Title layer_range_tree
// @Description
// @Author  == 琳琳也叫王木木 ==  2021/9/16 14:15
// @Create  2021/9/16 14:15
package layer_range_tree

import "math"

func largestValues(root *TreeNode) []int {
	if root == nil {
		return nil
	}
	ans := []int{}
	queue := []*TreeNode{root}
	for len(queue) > 0 {
		size := len(queue)
		max := int(math.Inf(-1)) //负无穷
		for i := 0; i < size; i++ {
			node := queue[0]
			queue = queue[1:]
			if node.Val > max {
				max = node.Val
			}
			if node.Left != nil {
				queue = append(queue, node.Left)
			}
			if node.Right != nil {
				queue = append(queue, node.Right)
			}
		}
		ans = append(ans, max)
	}

	return ans
}
